package com.example.controller;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.example.common.R;
import com.example.entity.Buyer;
import com.example.service.BuyerService;
import org.apache.logging.log4j.util.Strings;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletRequest;

@RestController
@RequestMapping("buyers")
public class BuyerController {

    private R r;


    @Autowired
    private BuyerService buyerService;

    /**
     * 采购员登录
     * @return
     */
    @PostMapping("/loginBuyer")
    public R login(HttpServletRequest request, @RequestBody Buyer buyer){

        LambdaQueryWrapper<Buyer> lqw= new LambdaQueryWrapper<>();
        lqw.eq(Strings.isNotEmpty(buyer.getJobNumber()),Buyer::getJobNumber,buyer.getJobNumber());

        Buyer buyer1 = buyerService.getOne(lqw);

        if (buyer1 != null) {
            return R.success("登录成功！",buyer1);

        }else {
            return R.error("登录失败！");
        }
    }

    /**
     * 分页查询所有采购员信息
     * @return
     */

    @GetMapping("/getPageBuyer/{currentPage}/{pageSize}")
    public R getPage(@PathVariable int currentPage,@PathVariable int pageSize,Buyer buyer){

        IPage page=new Page(currentPage,pageSize);
        LambdaQueryWrapper<Buyer> lqw =new LambdaQueryWrapper<>();
        lqw.like(Strings.isNotEmpty(buyer.getJobNumber()),Buyer::getJobNumber,buyer.getJobNumber());

        IPage page1 = buyerService.page(page,lqw);


        return R.success("分页查询所有buyers成功！",page1);
    }



//    /**
//     * 查询当前采购员
//     * @return
//     */
//
//    @GetMapping("/getMyself/{id}")
//
//    public R getOne(@PathVariable int id){
//
//        Buyer buyer= buyerService.getOne(id);
//
//        return R.success("分页查询所有buyer成功！",buyer);
//    }
//
//
    /**
     * 添加
     * @param buyer
     * @return
     */
    @PostMapping("/addBuyer")
    public R add(@RequestBody Buyer buyer){

        LambdaQueryWrapper<Buyer> lqw=new LambdaQueryWrapper<>();
        lqw.eq(Strings.isNotEmpty(buyer.getJobNumber()),Buyer::getJobNumber,buyer.getJobNumber());
        Buyer buyer1=buyerService.getOne(lqw);

        if (buyer1==null) {
            Boolean update = buyerService.save(buyer);
            if (update) {
                return R.success("添加buyer成功!", null);
            }
        }
        return R.error("工号已存在");
    }

    /**
     * 通过id删除
     * @param id
     * @return
     */
    @DeleteMapping("/deleteBuyer/{id}")
    public R deleteById(@PathVariable int id){

        Boolean byId = buyerService.removeById(id);
        if (byId){
            return R.success("删除buyers成功!",null);
        }
        return R.error("删除buyers失败!");
    }

    /**
     * 修改
     * @param buyer
     * @return
     */
    @PutMapping("/updateBuyer")
    public R deleteById(@RequestBody Buyer buyer){

        Boolean update = buyerService.updateById(buyer);
        if (update){
            return R.success("修改buyer成功!",buyer);
        }
        return R.error("修改buyer失败!");
    }


}

